package cre.tomcat.ch05.startup;

import cre.tomcat.ch05.core.SimpleLoader;
import cre.tomcat.ch05.core.SimpleWrapper;
import cre.tomcat.ch05.valve.ClientIpLoggerValve;
import cre.tomcat.ch05.valve.HeaderLoggerValve;
import org.apache.catalina.Container;
import org.apache.catalina.Loader;
import org.apache.catalina.connector.http.HttpConnector;

/**
 * tomcat bootstrap
 *
 * @Author penglai
 * @Date 2017/1/4.
 */
public class Bootstrap1 {

    public static void main(String[] args) {
        HttpConnector connector = new HttpConnector();
        Container container = createContainer();
        try {
            connector.setContainer(container);
            connector.initialize();
            connector.start();

            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Container createContainer() {
        SimpleWrapper wrapper = new SimpleWrapper();
        wrapper.setServletClass("cre.tomcat.ch03.servlet.ModernServlet");
        Loader loader = new SimpleLoader();
        wrapper.setLoader(loader);

        ClientIpLoggerValve clientIpLoggerValve = new ClientIpLoggerValve();
        HeaderLoggerValve headerLoggerValve = new HeaderLoggerValve();
        wrapper.addValve(clientIpLoggerValve);
        wrapper.addValve(headerLoggerValve);

        return wrapper;
    }
}
